package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    private MemberService memberService;

    @RequestMapping("/pageQuery")
    private PageResult pageQuery(@RequestBody QueryPageBean queryPageBean){
            return memberService.pageQuery(queryPageBean);

    }

    @RequestMapping("/add")
    public Result add(@RequestBody Member member){
        try {
            memberService.add(member);
            return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    @RequestMapping("/getById")
    public Result getById(Integer id){
        try {
            Member member = memberService.getById(id);
            return new Result(true,MessageConstant.GET_MEMBER_SUCCESS,member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_FALL);
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Member member){
        try {
            memberService.update(member);
            return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){

        try {
            memberService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

}
